﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Xaml.Behaviors.Layout;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using UserControlLib.Enums;
using UserControlLib.Models;

namespace Wpf_Examples.ViewModels
{
    public class LogPanelViewModel : ObservableObject
    {

        #region 属性
        /// <summary>
        /// UI日志
        /// </summary>
        private Dictionary<string, IEnumerable<object>> logTabItemsSource;
        public Dictionary<string, IEnumerable<object>> LogTabItemsSource
        {
            get => logTabItemsSource;
            set => SetProperty(ref logTabItemsSource, value);
        }

        /// <summary>
        ///     上传日志消息
        /// </summary>
        public ObservableCollection<LogIteamModel> UploadMessage { get; set; } = new ObservableCollection<LogIteamModel>();

        /// <summary>
        ///     通讯日志消息
        /// </summary>
        public ObservableCollection<LogIteamModel> SocketMessage { get; set; } = new ObservableCollection<LogIteamModel>();

        /// <summary>
        ///     系统信息
        /// </summary>
        public ObservableCollection<LogIteamModel> SystemMessage { get; set; } = new ObservableCollection<LogIteamModel>();

        #endregion

        #region 综合日志面板
        /// <summary>
        /// UI日志
        /// </summary>
        private Dictionary<string, IEnumerable<object>> singleLogTabItemsSource;
        public Dictionary<string, IEnumerable<object>> SingleLogTabItemsSource
        {
            get => singleLogTabItemsSource;
            set => SetProperty(ref singleLogTabItemsSource, value);
        }

        /// <summary>
        /// 综合日志信息
        /// </summary>
        public ObservableCollection<LogIteamModel> SingelSystemMessage { get; set; } = new ObservableCollection<LogIteamModel>();
        #endregion


        #region 命令

        public RelayCommand<string> ButtonCmd { get; set; }
        #endregion


        public LogPanelViewModel()
        {
            ButtonCmd = new RelayCommand<string>(ButtonMenu);
            LogPanelInit();
        }

        private void ButtonMenu(string obj)
        {
            switch (obj)
            {
                case "上传日志":
                    //分类日志分开打印
                    SendUploadMessage("新增一条上传日志", LogType.None);
                    SendUploadMessage("上传数据成功日志", LogType.Friendly);
                    SendUploadMessage("上传数据失败日志", LogType.Vigilant);

                    //综合日志统一打印
                    SendSystemMessage("新增一条上传日志", LogType.None);
                    SendSystemMessage("上传数据成功日志", LogType.Friendly);
                    SendSystemMessage("上传数据失败日志", LogType.Vigilant);
                    break;
                case "通讯日志":
                    SendSocketMessage("新增一条通讯日志", LogType.None);
                    SendSocketMessage("设备连接成功日志", LogType.Friendly);
                    SendSocketMessage("设备连接失败日志", LogType.Vigilant);

                    //综合日志统一打印
                    SendSystemMessage("新增一条通讯日志", LogType.None);
                    SendSystemMessage("设备连接成功日志", LogType.Friendly);
                    SendSystemMessage("设备连接失败日志", LogType.Vigilant);
                    break;
                case "系统日志":
                    SendSystemMessage("系统初始化完成", LogType.None);
                    SendSystemMessage("系统启动成功", LogType.Friendly);
                    SendSystemMessage("系统启动失败", LogType.Vigilant);
                    break;
            }
        }

        #region 日志处理方法
        private void LogPanelInit()
        {
            //分类日志初始化
            var logTabItemsSource = new Dictionary<string, IEnumerable<object>>();
            logTabItemsSource.Add("上传日志", UploadMessage);
            logTabItemsSource.Add("通讯日志", SocketMessage);
            logTabItemsSource.Add("系统日志", SystemMessage);
            LogTabItemsSource = logTabItemsSource;


            //综合日志初始化
            var singlelogTabItemsSource = new Dictionary<string, IEnumerable<object>>();
            singlelogTabItemsSource.Add("运行日志", SystemMessage);
            SingleLogTabItemsSource = singlelogTabItemsSource;
        }
        /// <summary>
        /// 上传日志消息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="type"></param>
        public void SendUploadMessage(string msg, LogType type = LogType.None)
        {
            PrintLog(UploadMessage, msg, type);
        }

        /// <summary>
        ///     通讯日志消息
        /// </summary>
        public void SendSocketMessage(string msg, LogType type = LogType.None)
        {
            PrintLog(SocketMessage, msg, type);
        }
        /// <summary>
        ///     系统信息
        /// </summary>
        public void SendSystemMessage(string msg, LogType type = LogType.None)
        {
            PrintLog(SystemMessage, msg, type);
        }

        private void PrintLog(ObservableCollection<LogIteamModel> list, string msg, LogType type = LogType.None)
        {
            Application.Current.Dispatcher.BeginInvoke(new Action(delegate
            {
                try
                {
                    if (list.Count > 300)
                    {
                        for (int i = 0; i < 150; i++)
                        {
                            list.RemoveAt(list.Count - 1);
                        }
                    }
                    var txt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "---" + msg;
                    var logIteam = new LogIteamModel
                    {
                        LogMsg = txt,
                        LogTyp = type
                    };
                    list.Insert(0, logIteam);
                }
                catch (Exception ex)
                {
                    //LogHelper.logSysErr.Error(ex);
                }
            }));
        }
        #endregion
    }
}
